package com.intellagric.agriecom.service.agriecom_produce_param;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.intellagric.agriecom.module.agriecom_produce_param.ParamService;
import com.intellagric.common.pojo.LayuiDataGridResult;
import com.intellagric.common.pojo.ResponseMessage;
import com.intellagric.dao.agriecom.agriecom_produce_param.AgriecomProduceParamDao;
import com.intellagric.mapper.AgriecomProduceParamMapper;
import com.intellagric.pojo.AgriecomProduceParam;
import com.intellagric.pojo.AgriecomProduceParamExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.List;


/**
 * @Autuor cky
 * @description
 * @Date 16:46 2019/3/8
 */
@Service
public class ParamServiceImpl implements ParamService {
    @Autowired
    private AgriecomProduceParamMapper paramMapper;

    @Autowired
    private AgriecomProduceParamDao dao;

    /**
     * 添加
     * @param vo
     * @return
     */
    @Override
    public ResponseMessage addParam(AgriecomProduceParam vo) {
        int i=0;
        if(vo.getId()!=null&&vo.getId()!="")
            i=paramMapper.insert(vo);
        if (i!=0)
            return ResponseMessage.success();
        return ResponseMessage.fail();
    }

    /**
     * 根据id查找
     * @param id
     * @return
     */
    @Override
    public AgriecomProduceParam selectParamById(String id) { return paramMapper.selectByPrimaryKey(id); }

    /**
     * 根据id删除
     * @param id
     * @return
     */
    @Override
    public LayuiDataGridResult deleteParamById(String id) {
        int i=paramMapper.deleteByPrimaryKey(id);
        if(i==1) {
            dao.relationDeleteByPrimaryKey(id);//级联删除
            return LayuiDataGridResult.success();
        }
        return LayuiDataGridResult.fail();
    }

    @Override
    public LayuiDataGridResult batchDelete(String[] ids) {
        int i=dao.batchDelete(ids);
        if(i!=0) {
            dao.batchRelationDelete(ids);//级联删除
            return LayuiDataGridResult.success();
        }
        return LayuiDataGridResult.fail();
    }

    /**
     * 匹配id修改
     * @param vo
     * @return
     */
    @Override
    public ResponseMessage updateParamByVo(AgriecomProduceParam vo) {
        AgriecomProduceParamExample example=new AgriecomProduceParamExample();
        example.createCriteria().andIdEqualTo(vo.getId());
        int i=paramMapper.updateByExampleSelective(vo,example);
        if(i==1)
            return ResponseMessage.success();
        return ResponseMessage.fail();
    }

    /**
     * 分页获取所有内容
     * @param page
     * @param limit
     * @return
     */
    @Override
    public LayuiDataGridResult getParamList(int page, int limit) {
        PageHelper.startPage(page,limit);
        AgriecomProduceParamExample example=new AgriecomProduceParamExample();
        example.createCriteria().andCategoryIdIsNotNull();
        PageInfo info=new PageInfo(paramMapper.selectByExample(example));
        return LayuiDataGridResult.success().add(info.getList(),info.getTotal());
    }

    /**
     * 根据分类获取分页
     * @param categoryId
     * @param page
     * @param limit
     * @return
     */
    @Override
    public LayuiDataGridResult getParamListByCategory(String categoryId, int page, int limit) {
        PageHelper.startPage(page,limit);
        AgriecomProduceParamExample example=new AgriecomProduceParamExample();
        example.createCriteria().andCategoryIdEqualTo(categoryId);
        List<AgriecomProduceParam> list=paramMapper.selectByExample(example);
        PageInfo info=new PageInfo(list);
        return LayuiDataGridResult.success().add(info.getList(),info.getTotal());
    }

    @Override
    public LayuiDataGridResult getParamListByCategory(String categoryId) {
        AgriecomProduceParamExample example=new AgriecomProduceParamExample();
        example.createCriteria().andCategoryIdEqualTo(categoryId);
        List<AgriecomProduceParam> list=paramMapper.selectByExample(example);
        return LayuiDataGridResult.success().add(list,list.size());
    }

    /**
     * 获得关联结果集
     * @param page
     * @param limit
     * @return
     */
    @Override
    public LayuiDataGridResult getRelationParamList(int page, int limit) {
        PageHelper.startPage(page,limit);
        List<AgriecomProduceParam> list=dao.relationSelect();
        PageInfo info=new PageInfo(list);
        return LayuiDataGridResult.success().add(info.getList(),info.getTotal());
    }

    @Override
    public LayuiDataGridResult getListByKeyword(int page, int limit, String keyword) {
            PageHelper.startPage(page,limit);
            AgriecomProduceParamExample paramExample=new AgriecomProduceParamExample();
            paramExample.createCriteria().andParamNameLike("%"+keyword+"%");
            List<AgriecomProduceParam> list=paramMapper.selectByExample(paramExample);
           PageInfo info=new PageInfo(list);
        return LayuiDataGridResult.success().add(info.getList(),info.getTotal());
    }
}
